package com.mall.order.services;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.order.OrderQueryService;
import com.mall.order.constant.OrderRetCode;
import com.mall.order.converter.OrderConverter;
import com.mall.order.dal.entitys.Order;
import com.mall.order.dal.entitys.OrderItem;
import com.mall.order.dal.entitys.OrderShipping;
import com.mall.order.dal.persistence.OrderItemMapper;
import com.mall.order.dal.persistence.OrderMapper;
import com.mall.order.dal.persistence.OrderShippingMapper;
import com.mall.order.dto.*;
import com.mall.order.utils.ExceptionProcessorUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 *  ciggar
 * create-date: 2019/7/30-上午10:04
 */
@Slf4j
@Component
@Service
public class OrderQueryServiceImpl implements OrderQueryService {
    @Autowired
    OrderMapper orderMapper;

    @Autowired
    OrderItemMapper orderItemMapper;

    @Autowired
    OrderShippingMapper orderShippingMapper;

    @Autowired
    OrderConverter orderConverter;

    @Override
    public OrderListResponse queryOrderList(OrderListRequest orderListRequest) {
        OrderListResponse orderListResponse = new OrderListResponse();

        try {
            // 检查请求参数是否合法
            orderListRequest.requestCheck();
            PageHelper.startPage(orderListRequest.getPage(), orderListRequest.getSize());
            Example example = new Example(Order.class);
            example.createCriteria()
                    .andEqualTo("userId", orderListRequest.getUserId());
            example.setOrderByClause("create_time desc");
            if (orderListRequest.getSort() != null) {
                example.setOrderByClause(orderListRequest.getSort());
            }
            // 查询用户的所有订单
            List<Order> orders = orderMapper.selectByExample(example);

            ArrayList<OrderDetailInfo> orderDetailInfos = new ArrayList<>();
            for (Order order : orders) {
                // 将Order转换为OrderDetailInfo
                OrderDetailInfo orderDetailInfo = orderConverter.order2detail(order);

                // 根据orderId查询orderItemList
                Example orderItemExample = new Example(OrderItem.class);
                orderItemExample.createCriteria()
                        .andEqualTo("orderId", order.getOrderId());
                List<OrderItem> orderItems = orderItemMapper.selectByExample(orderItemExample);

                // 将orderItem列表转换为OrderItemDto
                List<OrderItemDto> orderItemDtos = orderConverter.item2dto(orderItems);
                // 设置orderDetailInfo的orderItemDtos
                orderDetailInfo.setOrderItemDto(orderItemDtos);

                // 根据orderId查询orderShipping
                OrderShipping orderShipping = orderShippingMapper.selectByPrimaryKey(order.getOrderId());

                // 将orderShipping转换为OrderShippingDto
                OrderShippingDto orderShippingDto = orderConverter.shipping2dto(orderShipping);
                // 设置orderDetailInfo的orderShippingDto
                orderDetailInfo.setOrderShippingDto(orderShippingDto);

                orderDetailInfos.add(orderDetailInfo);
            }

            PageInfo<Order> orderPageInfo = new PageInfo<>(orders);
            orderListResponse.setTotal(orderPageInfo.getTotal());
            orderListResponse.setDetailInfoList(orderDetailInfos);
            orderListResponse.setCode(OrderRetCode.SUCCESS.getCode());
            orderListResponse.setMsg(OrderRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            log.info("OrderQueryServiceImpl#queryOrderList occur Exception：" + e);
            ExceptionProcessorUtils.wrapperHandlerException(orderListResponse, e);
            //e.printStackTrace();
        }

        return orderListResponse;
    }

    @Override
    public OrderDetailResponse2 queryOrderDetail(OrderDetailRequest orderDetailRequest) {
        OrderDetailResponse2 orderDetailResponse = new OrderDetailResponse2();

        try {
            // 检查请求参数是否合法
            orderDetailRequest.requestCheck();

            String orderId = orderDetailRequest.getOrderId();

            // 根据orderId查询订单
            Order order = orderMapper.selectByPrimaryKey(orderId);
            OrderShipping orderShipping = orderShippingMapper.selectByPrimaryKey(orderId);
            orderDetailResponse.setUserId(order.getUserId().intValue());
            orderDetailResponse.setUserName(order.getBuyerNick());
            orderDetailResponse.setOrderTotal(order.getPayment().doubleValue());
            orderDetailResponse.setTel(orderShipping.getReceiverPhone());
            orderDetailResponse.setStreetName(orderShipping.getReceiverAddress());
            orderDetailResponse.setOrderStatus(order.getStatus());

            Example orderItemExample = new Example(OrderItem.class);
            orderItemExample.createCriteria()
                    .andEqualTo("orderId", orderId);
            // 根据orderId查询订单商品关联信息
            List<OrderItem> orderItems = orderItemMapper.selectByExample(orderItemExample);
            // 将orderItems转换为orderItemDto
            List<OrderItemDto> orderItemDtos = orderConverter.item2dto(orderItems);

            orderDetailResponse.setGoodsList(orderItemDtos);
            orderDetailResponse.setCode(OrderRetCode.SUCCESS.getCode());
            orderDetailResponse.setMsg(OrderRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            log.info("OrderQueryServiceImpl#queryOrderDetail occur Exception：" + e);
            ExceptionProcessorUtils.wrapperHandlerException(orderDetailResponse, e);
            //e.printStackTrace();
        }

        return orderDetailResponse;
    }
}
